﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Forms;
using System.Runtime.InteropServices;

namespace MobileSketcher
{
    public partial class frmMain : Form
    {
        private int x1 = -1;
        private int y1 = -1;
        private int x2 = -1;
        private int y2 = -1;

        private Pen pen = new Pen(Color.Black);
        public Color DrawColor { get; set; }

        public frmMain()
        {
            InitializeComponent();

            DrawColor = Color.Black;
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            frmAbout aboutForm = new frmAbout();
            aboutForm.ShowDialog();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        private void menuItemPicture_Click(object sender, EventArgs e)
        {
            SelectPicture();
        }

        private void SelectPicture()
        {
            SelectPictureDialog selectPictureDialog = new SelectPictureDialog();
            DialogResult dialogResult = selectPictureDialog.ShowDialog();

            if (dialogResult != DialogResult.OK)
                return;

            pictureBox.Image = new Bitmap(selectPictureDialog.FileName);
        }

        private void menuItemClear_Click(object sender, EventArgs e)
        {
            pictureBox.CreateGraphics().Clear(Color.White);

            Reset();
        }

        private void Reset()
        {
            x1 = -1;
            y1 = -1;
            x2 = -1;
            y2 = -1;
        }

        private void panelRed_Click(object sender, EventArgs e)
        {
            DrawColor = Color.Red;
        }

        private void panelOrange_Click(object sender, EventArgs e)
        {
            DrawColor = Color.Orange;
        }

        private void panelYellow_Click(object sender, EventArgs e)
        {
            DrawColor = Color.Yellow;
        }

        private void panelGreen_Click(object sender, EventArgs e)
        {
            DrawColor = Color.Green;
        }

        private void panelBlue_Click(object sender, EventArgs e)
        {
            DrawColor = Color.Blue;
        }

        private void panelBrown_Click(object sender, EventArgs e)
        {
            DrawColor = Color.Brown;
        }

        private void panelWhite_Click(object sender, EventArgs e)
        {
            DrawColor = Color.Gainsboro;
        }

        private void panelBlack_Click(object sender, EventArgs e)
        {
            DrawColor = Color.Black;
        }

        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (pen.Color != DrawColor)
                pen = new Pen(DrawColor);

            x1 = x2;
            y1 = y2;
            x2 = e.X;
            y2 = e.Y;

            if (x1 == -1 && y1 == -1)
                return;

            pictureBox.CreateGraphics().DrawLine(pen, x1, y1, x2, y2);
        }

        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
          MessageBox.Show(String.Format("R={0}, G={1}, B={2}", DrawColor.R, DrawColor.G, DrawColor.B));

            Reset();
        }

        private void menuItemSave_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = saveFileDialog.ShowDialog();

            if (dialogResult != DialogResult.OK)
                return;

            Snapshot(saveFileDialog.FileName, pictureBox.CreateGraphics(), pictureBox.Bounds);
        }

        const int SRCCOPY = 0x00CC0020;

        // P/Invoke declaration
        [DllImport("coredll.dll")]
        public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);

        const int SW_MINIMIZED = 6;

        [DllImport("coredll.dll")]
        static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

        private void Snapshot(string filename, Graphics gx, Rectangle rect)
        {
            Bitmap bmp = new Bitmap(rect.Width, rect.Height);
            // Create compatible graphics
            Graphics gxComp = Graphics.FromImage(bmp);
            // Blit the image data
            BitBlt(gxComp.GetHdc(), 0, 0, rect.Width, rect.Height, gx.GetHdc(), rect.Left, rect.Top, SRCCOPY);
            bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
            // Cleanup
            bmp.Dispose();
            gxComp.Dispose();
        }
    }
}